package com.emergency.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emergency.config.BusinessException;
import com.emergency.data.entity.Department;
import com.emergency.data.entity.Purview;
import com.emergency.data.entity.User;
import com.emergency.data.param.UserParam;
import com.emergency.data.vo.UserVO;
import com.emergency.http.DefaultResponseCode;
import com.emergency.mapper.DepartmentMapper;
import com.emergency.mapper.PurviewMapper;
import com.emergency.mapper.UserMapper;
import com.emergency.service.UserService;
import com.emergency.util.JwtUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (User)用户服务实现类
 *
 * @author makejava
 * @since 2020-09-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private PurviewMapper purviewMapper;

    @Override
    public UserVO login(String username, String password) {
        //查询Mapper
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("username", username);
        query.last("LIMIT 1");
        //查询
        User user = userMapper.selectOne(query);
        if (user == null) {
            throw new BusinessException(DefaultResponseCode.USER_NOT_EXIST);
        }
        if (!password.equals(user.getPassword())) {
            throw new BusinessException(DefaultResponseCode.PASSWORD_NOT_CORRECT);
        }
        //VO
        UserVO vo = new UserVO();
        BeanUtil.copyProperties(user, vo);
        //查询部门
        Department department = departmentMapper.selectOne(new QueryWrapper<Department>()
                .eq("id", user.getDeptId())
                .last("LIMIT 1"));
        if (department != null) {
            vo.setDeptName(department.getDeptName());
        }
        //查询权限
        Purview purview = purviewMapper.selectOne(new QueryWrapper<Purview>()
                .eq("id", user.getPurviewId())
                .last("LIMIT 1"));
        if (purview != null) {
            vo.setPurviewName(purview.getPurviewName());
        }
        //生成token
        String token = JwtUtil.createJWT(IdUtil.randomUUID(), user.getId().toString(), null);
        vo.setToken(token);
        return vo;
    }

    @Override
    public boolean addUser(UserParam userParam) {
        User user = new User();
        BeanUtil.copyProperties(userParam, user);
        //默认密码123456，并加密
        user.setPassword(SecureUtil.md5("123456"));
        return userMapper.insert(user) == 1;
    }

    @Override
    public void changePassword(String username, String oldPassword, String newPassword) {
        //查询Mapper
        QueryWrapper<User> query = new QueryWrapper<>();
        query.eq("username", username);
        query.last("LIMIT 1");
        //查询
        User user = userMapper.selectOne(query);
        if (user == null) {
            throw new BusinessException(DefaultResponseCode.USER_NOT_EXIST);
        }
        if (!oldPassword.equals(user.getPassword())) {
            throw new BusinessException(DefaultResponseCode.PASSWORD_NOT_CORRECT);
        }
        if (newPassword.equals(user.getPassword())) {
            throw new BusinessException(DefaultResponseCode.PASSWORD_CAN_NOT_CONSISTENT);
        }
        user.setPassword(newPassword);
        userMapper.updateById(user);
    }

    @Override
    public boolean deleteUser(Long id) {
        //查询用户信息
        User user = userMapper.selectById(id);
        if (user == null) {
            throw new BusinessException(DefaultResponseCode.USER_NOT_EXIST);
        }
        return userMapper.deleteById(user) == 1;
    }
}
